package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwbqT;
import cn.e4j.modular.crm.mapper.GwbqTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuw
 * @since 2019-08-30
 */
@Service
public class GwbqTServiceImpl extends ServiceImpl<GwbqTMapper, GwbqT> {

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getUsersList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getUsersList(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public GwbqT getByKey(String GWbqENT, String GWbq001, String GWbqLEGAL) {
        UpdateWrapper<GwbqT> gwbqTUpdateWrapper = new UpdateWrapper<>();
        gwbqTUpdateWrapper.eq("GWBQENT", GWbqENT);
        gwbqTUpdateWrapper.eq("GWBQ001", GWbq001);
        gwbqTUpdateWrapper.eq("GWBQ004", GWbqLEGAL);
        GwbqT gwbq = this.baseMapper.selectOne(gwbqTUpdateWrapper);
        return gwbq;
    }

    @Transactional
    public void addData(GwbqT gwbqT) {
        Dto dto = Dtos.newDto();
        dto.put("ENT", gwbqT.getGWBQENT());
        dto.put("GWBQ001", gwbqT.getGWBQ001());
//        gwbqT.setGWBQ002(this.baseMapper.getGwbqSeq(dto));
        gwbqT.setGWBQ002(InvokeUtil.invokeBeanMethod("gwbqTMapper,getGwbqSeq",dto));
        gwbqT.insert();
    }

    @Transactional
    public void editData(GwbqT gwbqT) {
        //修改1
        UpdateWrapper<GwbqT> gwbqTUpdateWrapper = new UpdateWrapper<>();
        gwbqTUpdateWrapper.eq("GWBQENT", gwbqT.getGWBQENT());
        gwbqTUpdateWrapper.eq("GWBQ001", gwbqT.getGWBQ001());
        gwbqTUpdateWrapper.eq("GWBQ002", gwbqT.getGWBQ002());

        gwbqT.update(gwbqTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwbqT gwbqT) {
        delete(gwbqT);
    }

    @Transactional
    public void deleteDataBatch(List<GwbqT> gwbqTList) {
        for (int i = 0; i < gwbqTList.size(); i++) {
            delete(gwbqTList.get(i));
        }
    }

    private void delete(GwbqT gwbqT) {
        //修改1
        UpdateWrapper<GwbqT> gwbqTUpdateWrapper = new UpdateWrapper<>();
        gwbqTUpdateWrapper.eq("GWBQENT", gwbqT.getGWBQENT());
        gwbqTUpdateWrapper.eq("GWBQ001", gwbqT.getGWBQ001());
        gwbqTUpdateWrapper.eq("GWBQ002", gwbqT.getGWBQ002());

        gwbqT.delete(gwbqTUpdateWrapper);
    }

}
